package com.insight.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.insight.common.api.vo.Result;
import com.insight.common.aspect.annotation.AutoLog;
import com.insight.common.system.base.controller.JeecgController;
import com.insight.common.system.query.QueryGenerator;
import com.insight.modules.system.entity.SysPlatformType;
import com.insight.modules.system.service.ISysPlatformTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Description: 平台子系统
 * @author: yqkj
 * @Date: 2022-07-05
 * @Version: V1.0
 */
@Api(tags = "平台子系统")
@RestController
@RequestMapping("/sys/platformType")
@Slf4j
public class SysPlatformTypeController extends JeecgController<SysPlatformType, ISysPlatformTypeService> {
	@Autowired
	private ISysPlatformTypeService platformTypeService;

	/**
	 * 分页列表查询
	 *
	 * @param platformType
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "平台子系统-分页列表查询")
	@ApiOperation(value = "平台子系统-分页列表查询", notes = "平台子系统-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SysPlatformType>> queryPageList(SysPlatformType platformType,
														 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
														 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
														 HttpServletRequest req) {
		Page<SysPlatformType> page = new Page<SysPlatformType>(pageNo, pageSize);
		QueryWrapper<SysPlatformType> queryWrapper = QueryGenerator.initQueryWrapper(platformType, req.getParameterMap());
		queryWrapper.orderByAsc("sort").orderByDesc("create_time");
		IPage<SysPlatformType> pageList = platformTypeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param platformType
	 * @return
	 */
	@AutoLog(value = "平台子系统-添加")
	@ApiOperation(value = "平台子系统-添加", notes = "平台子系统-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysPlatformType platformType) {
		if (platformTypeService.count(new QueryWrapper<SysPlatformType>().eq("subsystem_value", platformType.getSubsystemValue())) > 0) {
			return Result.error("子系统编码已存在！");
		}
		platformTypeService.saveOrUpdate(platformType);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param platformType
	 * @return
	 */
	@AutoLog(value = "平台子系统-编辑")
	@ApiOperation(value = "平台子系统-编辑", notes = "平台子系统-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
	public Result<?> edit(@RequestBody SysPlatformType platformType) {
		if (platformTypeService.count(new QueryWrapper<SysPlatformType>().eq("subsystem_value", platformType.getSubsystemValue()).ne("id", platformType.getId())) > 0) {
			return Result.error("子系统编码已存在！");
		}
		platformTypeService.updateById(platformType);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "平台子系统-通过id删除")
	@ApiOperation(value = "平台子系统-通过id删除", notes = "平台子系统-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		platformTypeService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "平台子系统-批量删除")
	@ApiOperation(value = "平台子系统-批量删除", notes = "平台子系统-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		platformTypeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "平台子系统-通过id查询")
	@ApiOperation(value = "平台子系统-通过id查询", notes = "平台子系统-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		SysPlatformType platformType = platformTypeService.getById(id);
		if (platformType == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(platformType);
	}


//	/**
//	 * 导出excel
//	 *
//	 * @param request
//	 * @param actZNotification
//	 */
//	@RequestMapping(value = "/exportXls")
//	public ModelAndView exportXls(HttpServletRequest request, ActZNotification actZNotification) {
//		return super.exportXls(request, actZNotification, ActZNotification.class, "平台子系统");
//	}
//
//	/**
//	 * 通过excel导入数据
//	 *
//	 * @param request
//	 * @param response
//	 * @return
//	 */
//	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//		return super.importExcel(request, response, ActZNotification.class);
//	}

}
